/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.Configuration;
using CrSyncFilesUtils.Enums;

namespace CrSyncFilesUtils
{

    /// <summary>
    /// Class for parsing commandline arguments based on the original synctoy's cli.
    /// </summary>
    public class CrSynctoyCmdParser
    {
        public CrSynctoyCmdParser(FileSyncPairManager fileSyncPairManager, CrSyncToyManager synctoyManager,string metadataDirectory, string prefix, string logFilePath, string criticalLogFilePath)
        {
            PairManager = fileSyncPairManager;
            SyncToyManager = synctoyManager;
            MetaDataDirectory = metadataDirectory;
            Prefix = prefix;
            LogFilePath = logFilePath;
            CriticalLogFilePath = criticalLogFilePath;

            _crsyncConsole = new CrSyncFilesConsoleUtility(MetaDataDirectory,
                                                           Prefix,
                                                           PairManager,
                                                           SyncToyManager,
                                                           LogFilePath,
                                                           CriticalLogFilePath);
        }
        
        public string[] Arguments { get; set; }
        public FileSyncPairManager PairManager { get; set; }
        public CrSyncToyManager SyncToyManager { get; set; }
        public string MetaDataDirectory { get; set; }
        public string Prefix { get; set; }
        public string LogFilePath { get; set; }
        public string CriticalLogFilePath { get; set; }

        private CrSyncFilesConsoleUtility _crsyncConsole;
 

        public void Parse(string[] cmdArguments, SettingsBase settingsBase, string provider)
        {
            try
            {

                Arguments = cmdArguments;

                if (Arguments[0].ToLower().StartsWith("-d")) 
                {

                    // Create 

                    var arguments = string.Join(" ",Arguments);
                    var createDirectives = arguments.Substring(3, arguments.Length - 4);
                    var createArguments = createDirectives.Split(',');

                    var left = "";
                    var right = "";
                    var name = "";
                    var operation = FolderPairActionTypeEnum.Synchronize;
                    var checkContents = false;
                    var include = "*";
                    var exclude = "";

                    foreach (var argument in createArguments)
                    {
                        var args = argument.Split('=');
                        if (args[0].Trim().ToLower() == "left")
                        {
                            left = args[1].Trim();
                        }

                        if (args[0].Trim().ToLower() == "right")
                        {
                            right = args[1].Trim();
                        }

                        if (args[0].Trim().ToLower() == "name")
                        {
                            name = args[1].Trim();
                        }

                        if (args[0].Trim().ToLower() == "operation")
                        {
                            switch (args[1].Trim().ToLower())
                            {
                                case "synchronize":
                                    operation = FolderPairActionTypeEnum.Synchronize;
                                    break;
                                case "echo":
                                    operation = FolderPairActionTypeEnum.Echo;
                                    break;
                                case "contribute":
                                    operation = FolderPairActionTypeEnum.Contribute;
                                    break;
                                default:
                                    operation = FolderPairActionTypeEnum.Synchronize;
                                    break;
                            }

                        }

                        if (args[0].Trim().ToLower() == "check")
                        {
                            checkContents = args[1].Trim().ToLower() == "yes";
                        }

                        if ((args[0].Trim().ToLower() == "included") || (args[0].Trim().ToLower() == "include"))
                        {
                            include = args[1].Trim();
                        }

                        if ((args[0].Trim().ToLower() == "exclude") || (args[0].Trim().ToLower() == "excluded"))
                        {
                            exclude = args[1].Trim();
                        }
                    }

                    if (!PairManager.HasFolderPairNamed(name))
                    {
                        

                        _crsyncConsole.CreateFolderPair(name, left, right, operation, include, exclude, checkContents);
                    }
                    else
                    {
                        Console.WriteLine("A folder pair with that name already exists.");
                    }
                }
                else if (Arguments[0].ToLower().StartsWith("-u"))
                {
                    // I dunno why deleting a folder pair is "-u" if "-u" means update, then user should be able to update folder pair info not just deleting them
                    var allArguments = string.Join(" ", Arguments);
                    if(allArguments.Length >= 3)
                    {
                        var pairName = allArguments.Substring(2);
                        _crsyncConsole.DeleteFolderPairNamed(pairName.Trim());
                    }

                }
                else if(Arguments[0].StartsWith("-R"))
                {
                    var arguments = string.Join(" ", Arguments);
                    if (arguments.Trim().Length == 2)
                    {
                        _crsyncConsole.RunAllFolderPairs(false,settingsBase, provider );
                    }
                    else
                    {
                        var pairName = arguments.Trim().Substring(2);
                        _crsyncConsole.RunFolderPairNamed(pairName, false, settingsBase, provider);
                    }
                }
                else
                {
                    //TODO : Display help
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
    }
}
